package com.gsm.utils;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

import java.util.HashMap;
import java.util.Map;

public class SmsUtils {
    private static String accessKeyId = "LTAI4FykobtP7wzUJp127463";
    private static String accessSecret = "TQWObtf6mL8t3THxNLyvGrisaoEtMr";
    private static String RegionId = "cn-hangzhou";
    private static String SignName = "我要学Java";
    private static String TemplateCode = "SMS_160856798";

    public static Map<String, Object> sendCodeByIphoneNo(String iphoneNo, Integer code) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", RegionId);
        request.putQueryParameter("PhoneNumbers", iphoneNo);
        request.putQueryParameter("SignName", SignName);
        request.putQueryParameter("TemplateCode", TemplateCode);
        request.putQueryParameter("TemplateParam", "{\"code\":" + code + "}");
        Map<String, Object> map = new HashMap<>();
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            map.put("flag", true);
            map.put("resp", response);
            return map;
        } catch (Exception e) {
            map.put("flag", false);
            return map;
        }
    }
}